home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windows 95 API Bible
/
Windows 95 API Bible 3 Disc Set.iso
/
Win32 API Bible Book 2 of 3.iso
/
chapter9
/
crupdown.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-03-06
|
13KB
|
355 lines
#include <windows.h>
#include <commctrl.h>
#include "crupdown.h"
#if defined (WIN32)
#define IS_WIN32 TRUE
#else
#define IS_WIN32 FALSE
#endif
#define IS_NT IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
#define IS_WIN32S IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
#define IS_WIN95 (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
HINSTANCE hInst; // current instance
LPCTSTR lpszAppName = "MyApp";
LPCTSTR lpszTitle = "CreateUpDownControl()";
BOOL RegisterWin95( CONST WNDCLASS* lpwc );
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
HWND hWnd;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (hInstance, lpszAppName);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
wc.lpszMenuName = lpszAppName;
wc.lpszClassName = lpszAppName;
if ( IS_WIN95 )
{
if ( !RegisterWin95( &wc ) )
return( FALSE );
}
else if ( !RegisterClass( &wc ) )
return( FALSE );
hInst = hInstance;
hWnd = CreateWindow( lpszAppName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0,
NULL,
NULL,
hInstance,
NULL
);
if ( !hWnd )
return( FALSE );
ShowWindow( hWnd, nCmdShow );
UpdateWindow( hWnd );
while( GetMessage( &msg, NULL, 0, 0) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return( msg.wParam );
}
BOOL RegisterWin95( CONST WNDCLASS* lpwc )
{
WNDCLASSEX wcex;
wcex.style = lpwc->style;
wcex.lpfnWndProc = lpwc->lpfnWndProc;
wcex.cbClsExtra = lpwc->cbClsExtra;
wcex.cbWndExtra = lpwc->cbWndExtra;
wcex.hInstance = lpwc->hInstance;
wcex.hIcon = lpwc->hIcon;
wcex.hCursor = lpwc->hCursor;
wcex.hbrBackground = lpwc->hbrBackground;
wcex.lpszMenuName = lpwc->lpszMenuName;
wcex.lpszClassName = lpwc->lpszClassName;
// Added elements for Windows 95.
//...............................
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName,
IMAGE_ICON, 16, 16,
LR_DEFAULTCOLOR );
return RegisterClassEx( &wcex );
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
static HWND hEdit = NULL;
static HWND hUpDown = NULL;
switch( uMsg )
{
case WM_CREATE :
// Create the edit control.
//.........................
hEdit = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_BORDER | WS_VISIBLE,
40, 40, 70, 25, hWnd,
(HMENU)IDC_EDIT, hInst, NULL );
// Create the up-down control.
//............................
hUpDown = CreateUpDownControl( WS_CHILD | WS_BORDER |
WS_VISIBLE |
UDS_ALIGNRIGHT,
0, 0, 18, 25, hWnd, IDC_UPDOWN,
hInst, NULL,
100, 1, 1 );
// The buddy window could have been specified with the
// CreateUpDownControl() function. However for this
// example, we will use the UDM_SETBUDDY message.
//....................................................
SendMessage( hUpDown, UDM_SETBUDDY, (WPARAM)hEdit, 0 );
// Initialize the edit control to the beginning value.
//....................................................
SendMessage( hEdit, WM_SETTEXT, 0, (LPARAM)"1" );
break;
case WM_VSCROLL :
if ( (HWND)lParam == hUpDown )
{
HWND hBuddyWnd;
TCHAR szTmp[10];
// Retrieve the buddy window. This is useful if we
// are not sure which buddy window the scroll is for.
//...................................................
hBuddyWnd = (HWND)SendMessage( hUpDown, UDM_GETBUDDY, 0, 0 );
// Find out the base of the up/down control and
// format the string for the buddy window.
//.............................................
if ( SendMessage( hUpDown, UDM_GETBASE, 0, 0 ) == 10 )
wsprintf( szTmp, "%d", HIWORD( wParam ) );
else
wsprintf( szTmp, "%X", HIWORD( wParam ) );
// Set the new value into the edit control
// and make it the selected text.
//........................................
SendMessage( hBuddyWnd, WM_SETTEXT, 0, (LPARAM)szTmp );
SendMessage( hBuddyWnd, EM_SETSEL, 0, -1 );
}
break;
case WM_NOTIFY :
{
NM_UPDOWN* ud = (NM_UPDOWN*)lParam;
if ( ud->hdr.code == UDN_DELTAPOS )
{
// Disallow the value of 10 by modifying
// the iDelta value to skip over it.
//......................................
if ( (ud->iPos + ud->iDelta) == 10 )
{
if ( ud->iDelta > 0 )
ud->iDelta++;
else
ud->iDelta--;
}
}
}
break;
case WM_COMMAND :
switch( LOWORD( wParam ) )
{
case IDM_OPTIONS :
DialogBox( hInst, "OptionsDlg", hWnd, (DLGPROC)Options );
break;
case IDM_ABOUT :
DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
break;
case IDM_EXIT :
DestroyWindow( hWnd );
break;
}
break;
case WM_DESTROY :
PostQuitMessage(0);
break;
default :
return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
}
return( 0L );
}
LRESULT CALLBACK Options( HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
HWND hUpDown;
UDACCEL accel[2];
DWORD dwRange;
DWORD dwPos;
DWORD dwBase;
int nNumAccel;
// Retrieve the up/down control.
//..............................
hUpDown = GetDlgItem( GetParent( hDlg ), IDC_UPDOWN );
// Retrieve the acceleration information.
//.......................................
nNumAccel = SendMessage( hUpDown, UDM_GETACCEL,
2, (LPARAM)accel );
// Retrieve the range.
//....................
dwRange = SendMessage( hUpDown, UDM_GETRANGE, 0, 0 );
// Retrieve the current position.
//...............................
dwPos = SendMessage( hUpDown, UDM_GETPOS, 0, 0 );
// Retrieve the current base.
//...........................
dwBase = SendMessage( hUpDown, UDM_GETBASE, 0, 0 );
// Initialize the dialog controls.
//................................
SetDlgItemInt( hDlg, IDC_RANGE1, HIWORD( dwRange ), FALSE );
SetDlgItemInt( hDlg, IDC_RANGE2, LOWORD( dwRange ), FALSE );
SetDlgItemInt( hDlg, IDC_POS, LOWORD( dwPos ), FALSE );
SetDlgItemInt( hDlg, IDC_SEC1, accel[0].nSec, FALSE );
SetDlgItemInt( hDlg, IDC_INC1, accel[0].nInc, FALSE );
if ( nNumAccel > 1 )
{
SetDlgItemInt( hDlg, IDC_SEC2, accel[1].nSec, FALSE );
SetDlgItemInt( hDlg, IDC_INC2, accel[1].nInc, FALSE );
}
CheckDlgButton( hDlg, IDC_HEX,
dwBase == 16 ? BST_CHECKED : BST_UNCHECKED );
}
return (TRUE);
case WM_COMMAND:
switch( LOWORD( wParam ) )
{
case IDOK :
{
HWND hUpDown;
UDACCEL accel[2];
DWORD dwRange;
DWORD dwPos;
DWORD dwBase;
int nNumAccel;
// Retrieve the up/down control.
//..............................
hUpDown = GetDlgItem( GetParent( hDlg ), IDC_UPDOWN );
// Retrieve the information from the dialog.
//..........................................
dwRange = MAKELONG(
(WORD)GetDlgItemInt( hDlg, IDC_RANGE2, NULL, FALSE ),
(WORD)GetDlgItemInt( hDlg, IDC_RANGE1, NULL, FALSE ) );
dwPos = GetDlgItemInt( hDlg, IDC_POS, NULL, FALSE );
accel[0].nSec = GetDlgItemInt( hDlg, IDC_SEC1, NULL, FALSE );
accel[0].nInc = GetDlgItemInt( hDlg, IDC_INC1, NULL, FALSE );
accel[1].nSec = GetDlgItemInt( hDlg, IDC_SEC2, NULL, FALSE );
accel[1].nInc = GetDlgItemInt( hDlg, IDC_INC2, NULL, FALSE );
nNumAccel = 1;
if ( accel[1].nInc ) nNumAccel++;
if ( IsDlgButtonChecked( hDlg, IDC_HEX ) )
dwBase = 16;
else
dwBase = 10;
// Set the attributes for the up/down control.
//............................................
SendMessage( hUpDown, UDM_SETACCEL, nNumAccel, (LPARAM)accel );
SendMessage( hUpDown, UDM_SETBASE, dwBase, 0 );
SendMessage( hUpDown, UDM_SETPOS, 0, MAKELONG( (WORD)dwPos, 0 ) );
SendMessage( hUpDown, UDM_SETRANGE, 0, dwRange );
EndDialog( hDlg, IDOK );
}
break;
case IDCANCEL :
EndDialog( hDlg, IDCANCEL );
break;
}
break;
}
return (FALSE);
}
LRESULT CALLBACK About( HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return (TRUE);
case WM_COMMAND:
if ( LOWORD(wParam) == IDOK
|| LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, TRUE);
return (TRUE);
}
break;
}
return (FALSE);
}